package com.vsu.bookstore.mvc.ui.widget.fields;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.vaadin.easyuploads.UploadField;
import org.vaadin.imagefilter.FilterOperation;
import org.vaadin.imagefilter.Image;
import org.vaadin.imagefilter.filters.FitIntoFilter;
import org.vaadin.imagefilter.filters.RoundedCornersFilter;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class UploadImageField extends UploadField
{
  private final VerticalLayout imageContainer = new VerticalLayout();

  public UploadImageField()
  {
    super();
    setWidth("100%");
    setHeight("180px");
    setFieldType(FieldType.BYTE_ARRAY);
    setMargin(false);
    imageContainer.setMargin(true);
    imageContainer.setSpacing(false);
    addComponent(imageContainer);
    final Iterator<Component> it =  getComponentIterator();
    while (it.hasNext())
    {
      final Component child = it.next();
      if (child instanceof Upload)
        ((Upload)child).setButtonCaption("Upload Preview: ");
    }
  }

  @Override
  public void discard() throws SourceException
  {
    setValue(null);
    super.discard();
  }

  @Override
  public void setReadOnly(final boolean readOnly)
  {
    super.setReadOnly(readOnly);
    final Iterator<Component> it =  getComponentIterator();
    while (it.hasNext())
    {
      final Component child = it.next();
      if (!child.equals(imageContainer) &&
          !(child instanceof ProgressIndicator))
        child.setVisible(!readOnly);
    }
  }

  @Override
  protected void updateDisplay()
  {
    if (getValue() == null)
      return;
    final String mimeType = getLastMimeType();
    if (mimeType == null ||
        mimeType.equals("image/png") ||
        mimeType.equals("image/jpg") ||
        mimeType.equals("image/bmp") ||
        mimeType.equals("image/jpeg")||
        mimeType.equals("image/JPEG")||
        mimeType.equals("image/tiff")||
        mimeType.equals("image/BMP"))
    {
      final byte[] pngData = (byte[]) getValue();

      final Image image = new Image();
      image.load(pngData);
      FilterOperation op = FilterOperation.getByName(FilterOperation.FITINTO);
      final FitIntoFilter fif = (FitIntoFilter) op.getFilter();
      fif.setAllowEnlarging(true);
      fif.setHeight(130);
      fif.setWidth(80);
      image.addOperation(op);
      op = FilterOperation.getByName(FilterOperation.ROUNDEDCORNERS);
      final RoundedCornersFilter rcf = (RoundedCornersFilter) op.getFilter();
      rcf.setCornerRadius(9);
      image.addOperation(op);
      image.applyOperations();

      final InputStream stream = image.getInputStream();
      try
      {
        final byte[] bytes = new byte[stream.available()];
        stream.read(bytes);
        setValue(bytes);
      }
      catch (final IOException e)
      {
        e.printStackTrace();
      }
      imageContainer.removeAllComponents();
      imageContainer.addComponent(image);
      imageContainer.setComponentAlignment(image, Alignment.TOP_CENTER);
    }
  }
}
